bitwise = object:new()
bitwise.data32 = {}
for i = 1, 32 do
  bitwise.data32[i] = 2 ^ (32 - i)
end
function bitwise:d2b(num)
  local ba = {}
  if num / 2 >= self.data32[1] then
    print("BUG bitwise:d2b overflow: ", num)
    return nil
  end
  for i = 1, 32 do
    if num >= self.data32[i] then
      ba[i] = 1
      num = num - self.data32[i]
    else
      ba[i] = 0
    end
  end
  return ba
end
function bitwise:b2d(ba)
  local num = 0
  for i = 1, 32 do
    if ba[i] == 1 then
      num = num + 2 ^ (32 - i)
    end
  end
  return num
end
function bitwise:bclone(ba)
  local r = {}
  for i = 1, 32 do
    r[i] = ba[i]
  end
  return r
end
function bitwise:bcompare(op1, op2)
  local v
  for i = 1, 32 do
    v = op1[i] - op2[i]
    if v ~= 0 then
      return v
    end
  end
  return 0
end
function bitwise:bxor(op1, op2)
  local r = {}
  for i = 1, 32 do
    if op1[i] == op2[i] then
      r[i] = 0
    else
      r[i] = 1
    end
  end
  return r
end
function bitwise:dxor(a, b)
  local op1 = self:d2b(a)
  local op2 = self:d2b(b)
  local r = {}
  for i = 1, 32 do
    if op1[i] == op2[i] then
      r[i] = 0
    else
      r[i] = 1
    end
  end
  return self:b2d(r)
end
function bitwise:band(op1, op2)
  local r = {}
  for i = 1, 32 do
    if op1[i] == 1 and op2[i] == 1 then
      r[i] = 1
    else
      r[i] = 0
    end
  end
  return r
end
function bitwise:dand(a, b)
  local op1 = self:d2b(a)
  local op2 = self:d2b(b)
  local r = {}
  for i = 1, 32 do
    if op1[i] == 1 and op2[i] == 1 then
      r[i] = 1
    else
      r[i] = 0
    end
  end
  return self:b2d(r)
end
function bitwise:bor(op1, op2)
  local r = {}
  for i = 1, 32 do
    if op1[i] == 1 or op2[i] == 1 then
      r[i] = 1
    else
      r[i] = 0
    end
  end
  return r
end
function bitwise:dor(a, b)
  local op1 = self:d2b(a)
  local op2 = self:d2b(b)
  local r = {}
  for i = 1, 32 do
    if op1[i] == 1 or op2[i] == 1 then
      r[i] = 1
    else
      r[i] = 0
    end
  end
  return self:b2d(r)
end
function bitwise:bnot(op1)
  local r = {}
  for i = 1, 32 do
    if op1[i] == 1 then
      r[i] = 0
    else
      r[i] = 1
    end
  end
  return r
end
function bitwise:dnot(a)
  local op1 = self:d2b(a)
  local r = {}
  for i = 1, 32 do
    if op1[i] == 1 then
      r[i] = 0
    else
      r[i] = 1
    end
  end
  return self:b2d(r)
end
function bitwise:dnotwithposd(a, b)
  local op1 = self:d2b(a)
  local op2 = self:d2b(b)
  local r = {}
  for i = 1, 32 do
    if op2[i] == 1 then
      if op1[i] == 1 then
        r[i] = 0
      else
        r[i] = 1
      end
    else
      r[i] = op1[i]
    end
  end
  return self:b2d(r)
end
function bitwise:bright_slow(op1, n)
  if n > 32 or n < 0 then
    return self:d2b(0)
  end
  local r = bitwise:bclone(op1)
  for j = 1, n do
    for i = 31, 1, -1 do
      r[i + 1] = r[i]
    end
    r[1] = 0
  end
  return r
end
function bitwise:bright(op1, n)
  if n > 32 or n < 0 then
    return self:d2b(0)
  end
  local r = bitwise:bclone(op1)
  for i = 32, 1 + n, -1 do
    r[i] = r[i - n]
  end
  for i = 1, n do
    r[i] = 0
  end
  return r
end
function bitwise:dright(a, n)
  if n > 32 or n < 0 then
    return 0
  end
  local r = self:d2b(a)
  for i = 32, 1 + n, -1 do
    r[i] = r[i - n]
  end
  for i = 1, n do
    r[i] = 0
  end
  return self:b2d(r)
end
function bitwise:dright_slow(a, n)
  local op1 = self:d2b(a)
  local r = self:d2b(0)
  if n < 32 and n >= 0 then
    for j = 1, n do
      for i = 31, 1, -1 do
        op1[i + 1] = op1[i]
      end
      op1[1] = 0
    end
    r = op1
  end
  return self:b2d(r)
end
function bitwise:bleft(op1, n)
  if n > 32 or n < 0 then
    return self:d2b(0)
  end
  local r = bitwise:bclone(op1)
  for i = 1, 32 - n do
    r[i] = r[i + n]
  end
  for i = 33 - n, 32 do
    r[i] = 0
  end
  return r
end
function bitwise:dleft(a, n)
  if n > 32 or n < 0 then
    return 0
  end
  local op1 = self:d2b(a)
  for i = 1, 32 - n do
    op1[i] = op1[i + n]
  end
  for i = 33 - n, 32 do
    op1[i] = 0
  end
  return self:b2d(op1)
end
function bitwise:dleft_slow(a, n)
  local op1 = self:d2b(a)
  local r = self:d2b(0)
  if n < 32 and n >= 0 then
    for j = 1, n do
      for i = 1, 31 do
        op1[i] = op1[i + 1]
      end
      op1[32] = 0
    end
    r = op1
  end
  return self:b2d(r)
end
function bitwise:bstr(ba, sep)
  local str = ""
  for i = 1, 32 do
    str = str .. ba[i]
    if i % 4 == 0 and i ~= 32 and sep ~= nil then
      str = str .. sep
    end
  end
  return str
end
function bitwise:print(ta)
  local sr = ""
  for i = 1, 32 do
    sr = sr .. ta[i]
  end
  print(sr)
end
